/**
 * @file    Expression classname is undefined on line 2, column 15 in Templates/Classes/Class.java.
 * @author  Haseeb Yousaf
 * @see     LICENSE (MIT style license file)
 * @version 1.0
 * @date    Nov 10, 2011 11:57:48 AM
 */
package carrello.util;

import org.hibernate.Session;
import carrello.model.persist.EntityManagerImpl;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.*;
import org.apache.log4j.Logger;
import static java.lang.System.out;

public class EntityManagerUtil {
    /** Logging handle. */
    public static final Logger log = Logger.getLogger(EntityManagerUtil.class);
    
    /** This class is not intended to be instantiated. */
    private EntityManagerUtil() {

    }
    
    // ~~~~~~~~~~~~~~~~~ EntityManager stuff ~~~~~~~~~~~~~~~~~~~~~~~
    
    private static final Map<String, EntityManagerImpl> Entity_Managers = new HashMap<String, EntityManagerImpl>();
    
    /*
     * /************************************************************** Register
     * all default EMs here.
     */
    static {
        registerEntityManager("Carrello", new EntityManagerImpl());
    }
    
    /**
     * Looks up a Eurocarb data object using its canonical id.
     */
    public static final <T> T lookup(Class<T> c, int id) throws Exception {

        return getEntityManager().lookup(c, id);
    }
    
    /* getEntityManager *//****************************************
     * Returns the default EntityManager.
     */
    public static final EntityManagerImpl getEntityManager() {

        return getEntityManager("Carrello");
    }
    
    /**
     * Clone of getEntityManager(), just prevents the need to cast my hand.
     * 
     * @return
     */
    public static final EntityManagerImpl getHibernateEntityManager() {

        return  getEntityManager("Carrello");
    }
    
    public static final Session getHibernateSession() {

        return getHibernateEntityManager().getHibernateSession();
    }
    
    public static final org.hibernate.Query getHqlQuery(String query) {

        return getHibernateEntityManager().getHibernateSession().createQuery(query);
    }
    
    /* getEntityManager *//****************************************
     * Returns the EntityManager associated with the given
     * name. The default EntityManager can be obtained via the String name
     * "default".
     */
    static final EntityManagerImpl getEntityManager(String name) {

        assert name != null;
        EntityManagerImpl em = Entity_Managers.get(name);
        assert (em != null);
        return em;
    }
    
    /* registerEntityManager *//***********************************
     * Registers an EntityManager for use.
     */
    static final void registerEntityManager(String name, EntityManagerImpl em) {

        assert em != null;
        Entity_Managers.put(name, em);
    }
    
    public static void main(String[] args)
    {
        out.println("before..");
        getEntityManager().getHibernateSession();
        out.println("after...");
    }
}
